package cn.supermaster.management.core.component;

import java.io.Serializable;

import cn.supermaster.management.common.utils.CheckUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.supermaster.management.common.utils.TextUtils;
import cn.supermaster.management.core.session.OnlineSession;
import cn.supermaster.management.system.domain.SystemUserOnline;
import cn.supermaster.management.system.service.ISystemUserOnlineService;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.core.service
 * @ClassName: RegisterManager
 * @Description: [会话db操作处理器]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/11 0:38
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Component
public class UserOnlineManager {

    @Autowired
    private ISystemUserOnlineService onlineService;

    /**
     * @Description: [删除会话]
     * <p></P>
     * @Title: deleteSession
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: onlineSession  会话信息
     * @Return: void
     * @Throws:
     */
    public void deleteSession(OnlineSession onlineSession) {
        onlineService.deleteOnlineById(String.valueOf(onlineSession.getId()));
    }

    /**
     * @Description: [获取会话信息]
     * <p></P>
     * @Title: getSession
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: sessionId
     * @Return: org.apache.shiro.session.Session
     */
    public Session getSession(Serializable sessionId) {
        SystemUserOnline userOnline = onlineService.selectOnlineById(String.valueOf(sessionId));
        return CheckUtils.isNull(userOnline) ? null : createSession(userOnline);
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: createSession
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: userOnline
     * @Return: org.apache.shiro.session.Session
     */
    public Session createSession(SystemUserOnline userOnline) {
        OnlineSession onlineSession = new OnlineSession();
        if (CheckUtils.isNull(userOnline)) return onlineSession;

        //
        onlineSession.setId(userOnline.getSessionId());
        onlineSession.setHost(userOnline.getIpaddr());
        onlineSession.setBrowser(userOnline.getBrowser());
        onlineSession.setOs(userOnline.getOs());
        onlineSession.setDeptName(userOnline.getDeptName());
        onlineSession.setuserName(userOnline.getuserName());
        onlineSession.setStartTimestamp(userOnline.getStartTimestamp());
        onlineSession.setLastAccessTime(userOnline.getLastAccessTime());
        onlineSession.setTimeout(userOnline.getExpireTime());
        return onlineSession;
    }
}
